//
//  WContactUSVC.m
//  CBT
//
//  Created by apple on 2019/7/8.
//  Copyright © 2019 Mr.Zhang. All rights reserved.
//

#import "WContactUSVC.h"
#import "WCContactCell.h"

@interface WContactUSVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation WContactUSVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = kLocalizedString(@"联系我们",nil);
    
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 290;
    [self.tableView registerClass:[WCContactCell class] forCellReuseIdentifier:@"contactcell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    WCContactCell *cell = [WCContactCell createTableView:tableView];
    if (indexPath.row) {
        cell.nameLabel.text = kLocalizedString(@"微信", nil);
        cell.scanCodeImg.image = [UIImage imageNamed:@"ercode"];
    }
    else {
        cell.nameLabel.text = @"Facebook";
        cell.scanCodeImg.image = [UIImage imageNamed:@"ercode"];
    }
    [[cell.copBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"copy");
        [self saveScreenImage:cell.scanCodeImg];
    }];
    
    return cell;
}

-(void)saveScreenImage:(UIImageView *)imageView {
    UIImage * image = [self captureImageFromView:imageView];
    UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:didFinishSavingWithError:contextInfo:),nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        [HFStatusView showFailureMessage:kLocalizedString(@"图片保存失败",nil)];
    }
    else {
        [HFStatusView showSuccessMessage:kLocalizedString(@"图片保存成功",nil)];
    }
}

-(UIImage *)captureImageFromView:(UIImageView *)view{
    
    UIGraphicsBeginImageContextWithOptions(view.frame.size,NO, 0);
    
    [[UIColor clearColor] setFill];
    [[UIBezierPath bezierPathWithRect:view.bounds] fill];
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:ctx];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

- (UITableView *)tableView {
    if (!_tableView) {
        CGRect rect = self.view.frame;
        rect.size.height -= HFNavigationHeight;
        _tableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
        
        _tableView.delegate     = self;
        _tableView.dataSource   = self;
        _tableView.scrollsToTop = YES;
        
        _tableView.separatorColor  = [UIColor clearColor];
        _tableView.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
        
        if (@available(iOS 11.0, *)){
            self.tableView.estimatedRowHeight = 0;
            self.tableView.estimatedSectionHeaderHeight = 0;
            self.tableView.estimatedSectionFooterHeight = 0;
        }
    }
    
    return _tableView;
}


@end
